package com.wanxi.servlet.major;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.MajorModel;
import com.wanxi.service.Impl.MajorServiceImpl;
import com.wanxi.service.MajorService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/15 15:56
 */
@WebServlet("/back/UpdateMajor")
public class UpdateMajorServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //获取前端数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String image = req.getParameter("image");
//        String isShow = req.getParameter("isShow");
        String enable = req.getParameter("enable");
        String remark = req.getParameter("remark");
        String majorId = req.getParameter("majorId");

        //封装model
        MajorModel majorModel = MajorModel.builder()
                .id(Tool.nullToInt(id))
                .name(Tool.nullToStr(name))
                .image(Tool.nullToStr(image))
//                .isShow(Tool.nullToInt(isShow))
                .enable(Tool.nullToInt(enable))
                .remark(Tool.nullToStr(remark))
                .majorId(Tool.nullToInt(majorId))
                .build();
        //调用service层
        MajorService majorService = new MajorServiceImpl();
        ResultDto resultDto = majorService.update(majorModel);
        //返回数据给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDto));

    }
}
